package com.example.mymap_beta;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay; 

public class MyMap_Beta extends MapActivity {

	


	// const
	private static final double [] START_COORDINATE	  = {21.025496,105.853186};	
	private static final double [] SPECIAL_COORDINATE = {20.996311,105.862316}; 
	private static final int	MAP_VIEW_CENTER_LAYOUT= 0;
	private static final int	DIRECT_OPTION_CENTER_LAYOUT = 1;
	private static final int 	WEATHER_VIEW_CENTER_LAYOUT =2;
	private static final int 	DIRECTION_LIST_CENTER_LAYOUT =3;
	
	private static final int	TEXTVIEW_TOP_LAYOUT = 0;
	private static final int	EDIT_TEXT_TOP_LAYOUT = 1;
	private static final int	FIRST_CONTROL_BOTTOM_LAYOUT = 0;
	private static final int	SECOND_CONTROL_BOTTOM_LAYOUT = 1;
	private static final int	THIRD_CONTROL_BOTTOM_LAYOUT  = 2;
	private static final int 	FOURTH_CONTROL_BOTTOM_LAYOUT  = 3;
	private static final int 	BACK_CONTROL_BOTTOM_LAYOUT  = 5;
	
	
	final CharSequence[] items = {"Direct from here","Direct to here","Set Special Location", "Weather in "};
	int item_pick ;
	boolean start_from_myLocation = true;
	
	protected static String GPS_FILTER = "GPS_LOCATION";
	private long mEventStartTime,mEventEndTime;
	private final long LONG_TIME = 1000; // 1 s
	
	// Component
	ImageButton 		btn_map,btn_search,btn_direction ;
	EditText   			edit_search,edit_pick_start_point,edit_pick_end_point;
	TextView			txt_info,weather_title,weather_detail;
	ImageButton			btn_current_location;
	ViewFlipper 		vf_top,vf_center,vf_bottom;
	View				dialog_custom;
	ImageView			weather_image;
	Button				btn_getDirection,btn_getDirection1,btn_weather, btn_direction_list,btn_back;
	
	
	protected MapView				mv;
	protected MapController			mc;
	
	protected Location				touchLocation,aLocation,bLocation;
	protected int mode = 0;
	protected static Location 		specialLocation;
	protected GeoPoint gp;
	
	//protected GeoPoint 				currentGeoPoint;
	protected List<Overlay> 		listOfOverlays;
 	protected MyIconOverLay 		A_Overlay,B_Overlay,Special_Overlay;
 	protected MyCustomLocationOverlay 	myCustomLocationOverlay; 
 	protected RouteOverlay routeOverlay;
	
 	
 	AlertDialog			alertDialog;
	
 	String info_direction ;				
	String distance ;
	String duration;;
	ArrayList<String> directionList ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        MyIconOverLay.setContext(getApplicationContext());
        MyCustomLocationOverlay.setContext(getApplicationContext());
        
        //////////////
        mv = (MapView)findViewById(R.id.myMap);
        mc = mv.getController();
        listOfOverlays = mv.getOverlays();
        ///////////////
       
        /*intentMyService = new Intent(this,MyGpsService.class);
        service = startService(intentMyService);
        
        IntentFilter mainFilter = new IntentFilter(GPS_FILTER);
        receive = new MyMainLocalReceiver();
        registerReceiver(receive, mainFilter);
        */
        
        myCustomLocationOverlay = new MyCustomLocationOverlay(this, mv);
        
        
        
        
        listOfOverlays.clear();
        listOfOverlays.add(myCustomLocationOverlay);
        

        ///////////////////////////////////////////////////////////////////////////////
        
        initComponent();
        
        
        
    }// end Oncreate
    
    
    

    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		myCustomLocationOverlay.disableMyLocation();
		myCustomLocationOverlay.disableCompass();
		
	} 

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		promptEnableGPS();
		myCustomLocationOverlay.enableMyLocation();
		myCustomLocationOverlay.disableCompass(); 
	} 

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			if(vf_top.getDisplayedChild()==TEXTVIEW_TOP_LAYOUT 
					&& vf_center.getDisplayedChild()==MAP_VIEW_CENTER_LAYOUT
					&& vf_bottom.getDisplayedChild()==FIRST_CONTROL_BOTTOM_LAYOUT)
			{
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
				 
				// set title
				alertDialogBuilder.setTitle("Do you sure exit");
	 
				// set dialog message
				alertDialogBuilder 
					.setCancelable(false)
					.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) { 
							MyMap_Beta.this.finish();
						}
					  })
					.setNegativeButton("No",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) { 
							dialog.cancel();
						}
					}); 
					AlertDialog alertDialog = alertDialogBuilder.create(); 
					alertDialog.show();
			}
			
			resetSomething();
			
			return false;
		}
		
		return super.onKeyDown(keyCode, event);
	}




	@Override
	public boolean dispatchTouchEvent(MotionEvent motionEvent) {
		// TODO Auto-generated method stub
		if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
		{
			mEventStartTime = (motionEvent.getPointerCount() >= 2)? 0 :motionEvent.getEventTime();
			
			Log.e("<<Motion Down>>", "count : "+motionEvent.getPointerCount()+" time" + mEventStartTime);
			
			
			//
		}else if(motionEvent.getAction() == MotionEvent.ACTION_POINTER_DOWN)
		{
			///
			
			
		}
		
		else if((motionEvent.getAction() == MotionEvent.ACTION_UP))
		{
			mEventEndTime  	= motionEvent.getEventTime();
			if( (mEventEndTime - mEventStartTime >= LONG_TIME) && mEventStartTime != 0 )
			{
				if(vf_center.getDisplayedChild() == MAP_VIEW_CENTER_LAYOUT)
				{
				
				
				gp = mv.getProjection().fromPixels((int)motionEvent.getX(), (int)motionEvent.getY());
				//Toast.makeText(MyMap_Beta.this, "Start : "+mEventStartTime+ " End : "+mEventEndTime, Toast.LENGTH_LONG).show();
				Log.e("<<Motion UP>>", "X : "+ motionEvent.getX()+ " Y : "+motionEvent.getY());
				Geocoder gc = new Geocoder(MyMap_Beta.this);
				try {
					List<Address> streets = gc.getFromLocation(gp.getLatitudeE6()/1E6, gp.getLongitudeE6()/1E6, 1);
					//Log.e("Motion up", ""+streets.toString());
					final Address retAddr = streets.get(0);
					Log.e("Motion up", ""+retAddr.getAddressLine(3)+"\n"+retAddr.toString());
					
						AlertDialog.Builder builder = new AlertDialog.Builder(this);
						builder.setTitle("Pick a color");
						builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int item) {
								// TODO Auto-generated method stub
								
								item_pick = item;
							}
						});
						
						builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								alertDialog.dismiss();
							}
						});
						
						builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
									
								switch (item_pick) {
								case 0: // Direct From Here
									
									
									aLocation.setLatitude(gp.getLatitudeE6()/1E6);
									aLocation.setLongitude(gp.getLongitudeE6()/1E6);
									A_Overlay.setGeoPoint(gp);
									listOfOverlays.add(A_Overlay);
									
									if(bLocation.getLatitude()!=0 && bLocation.getLongitude()!=0)
									{
										listOfOverlays.remove(routeOverlay);
										new GetDirection().execute();
										vf_bottom.setDisplayedChild(THIRD_CONTROL_BOTTOM_LAYOUT);
									}
									start_from_myLocation = false;
									
									break;
								case 1: // Direct To Here
									bLocation.setLatitude(gp.getLatitudeE6()/1E6);
									bLocation.setLongitude(gp.getLongitudeE6()/1E6);
									B_Overlay.setGeoPoint(gp);
									
									listOfOverlays.add(B_Overlay);
									
									if(start_from_myLocation)
									{
										aLocation.setLatitude(myCustomLocationOverlay.getMyLocation().getLatitudeE6()/1E6);
										aLocation.setLongitude(myCustomLocationOverlay.getMyLocation().getLongitudeE6()/1E6);
										
									}
									
										
										listOfOverlays.remove(routeOverlay);
										A_Overlay.setGeoPoint(new GeoPoint((int)(aLocation.getLatitude()*1E6), 
												(int)(aLocation.getLongitude()*1E6)));
										new GetDirection().execute();
										vf_bottom.setDisplayedChild(THIRD_CONTROL_BOTTOM_LAYOUT);
										
									

									
									break;
									
								case 2: // Set Special Location
									specialLocation.setLatitude(gp.getLatitudeE6()/1E6);
									specialLocation.setLongitude(gp.getLongitudeE6()/1E6);
									Special_Overlay.setGeoPoint(gp);
									
									
									
									break;
									
								case 3: // Weather
									
									
									getWeatherInfo();
									
									break;

								default:
									break;
								}
								
							}
						});
						
						
						
						alertDialog = builder.create();
						alertDialog.show();
						
					
					} 
				catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					}
				
				}
				
				
			}
		}
		
		
		
		
		return super.dispatchTouchEvent(motionEvent);
	}


 
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	private void initComponent()
	{
		
		aLocation		= new Location("Location A");
		bLocation		= new Location("Location B");
		specialLocation = new Location("Special Location");
		specialLocation.setLatitude(SPECIAL_COORDINATE[0]);
		specialLocation.setLongitude(SPECIAL_COORDINATE[1]);
		
		
		
		
		A_Overlay = new MyIconOverLay(R.drawable.ic_location_a);
		B_Overlay = new MyIconOverLay(R.drawable.ic_location_b);
		Special_Overlay = new MyIconOverLay(R.drawable.marker,new GeoPoint((int)(specialLocation.getLatitude()*1E6),
				(int)(specialLocation.getLongitude()*1E6)));
		listOfOverlays.add(Special_Overlay);
		
		vf_top 		= (ViewFlipper)findViewById(R.id.vf_top);
		vf_center	= (ViewFlipper)findViewById(R.id.vf_center);
		vf_bottom	= (ViewFlipper)findViewById(R.id.vf_bottom);
		
		
		weather_image	= (ImageView)findViewById(R.id.weather_image);
		btn_map 		= (ImageButton)findViewById(R.id.btn_map);
		btn_search		= (ImageButton)findViewById(R.id.btn_search);
		btn_direction	= (ImageButton)findViewById(R.id.btn_direction);
		btn_getDirection= (Button)findViewById(R.id.btn_get_direction);
		btn_getDirection1 =(Button)findViewById(R.id.btn_get_direction1);
		btn_weather 	= (Button)findViewById(R.id.btn_weather);
		btn_direction_list = (Button)findViewById(R.id.btn_direction_list);
		btn_back		= (Button)findViewById(R.id.btn_back);
		
		
        edit_search = (EditText)findViewById(R.id.edit_search);
        edit_pick_end_point = (EditText)findViewById(R.id.edit_pick_end_point);
        edit_pick_start_point = (EditText)findViewById(R.id.edit_pick_start_point);
        
        txt_info 		= (TextView)findViewById(R.id.txt_info);
        weather_title	= (TextView)findViewById(R.id.weather_title);
        weather_detail	= (TextView)findViewById(R.id.weather_detail);
        
        
        btn_current_location = (ImageButton)findViewById(R.id.btn_current_location);
        
        
        
        dialog_custom = LayoutInflater.from(this).inflate(R.layout.dialog_custom, null);
        
        
        btn_direction_list.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) { 
				vf_center.setDisplayedChild(DIRECTION_LIST_CENTER_LAYOUT);
				TextView tv_info_dir = (TextView)findViewById(R.id.direction_info); 
				TextView tv_duration = (TextView)findViewById(R.id.duration_distance_direction);
				ListView lv_direction_list = (ListView)findViewById(R.id.direction_listview);
				
				tv_info_dir.setText("Content: " + info_direction);
				tv_duration.setText("Distance: "+ distance + ", Duration: "+ duration ); 
				
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyMap_Beta.this,android.R.layout.simple_list_item_1, 
						android.R.id.text1, directionList);
				lv_direction_list.setAdapter(adapter);
				vf_bottom.setDisplayedChild(BACK_CONTROL_BOTTOM_LAYOUT);
			}
		});
        
        btn_back.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				vf_center.setDisplayedChild(MAP_VIEW_CENTER_LAYOUT);
				vf_bottom.setDisplayedChild(THIRD_CONTROL_BOTTOM_LAYOUT);
			}
		});
        
        /////
        btn_map.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				// Reset everything
				
				// reset something
				resetSomething();
			}
		});
        
        edit_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				Boolean handle = false;
				
				if(actionId == EditorInfo.IME_ACTION_SEARCH)
				{
					Geocoder gc = new Geocoder(MyMap_Beta.this);
					
					try {
						List<Address> lstAddress = gc.getFromLocationName(edit_search.getText().toString(), 1);
						
						if(lstAddress.size() > 0)
						{
							Toast.makeText(MyMap_Beta.this, "found address", Toast.LENGTH_SHORT).show();
							Address retAddr = lstAddress.get(0);
							
						bLocation.setLatitude(retAddr.getLatitude());
						bLocation.setLongitude(retAddr.getLongitude());
						gp = new GeoPoint((int)(retAddr.getLatitude()*1E6),(int)(retAddr.getLongitude()*1E6));
						
						
						//mc.animateTo(new GeoPoint((int)(bLocation.getLatitude()*1E6), (int)(bLocation.getLongitude()*1E6)));
						mc.animateTo(new GeoPoint((int)(retAddr.getLatitude()*1E6), (int)(retAddr.getLongitude()*1E6)));
						
						B_Overlay.setGeoPoint(new GeoPoint((int)(retAddr.getLatitude()*1E6), (int)(retAddr.getLongitude()*1E6)));
						listOfOverlays.add(B_Overlay);
						
						vf_bottom.setDisplayedChild(FOURTH_CONTROL_BOTTOM_LAYOUT);
						
						}
						else
							Toast.makeText(MyMap_Beta.this, "Not found address", Toast.LENGTH_SHORT).show();
						
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					
					handle = true;
				}
				
				return handle;
			}
		});
        
       btn_current_location.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
				goToMyLocation();
				
			}
       });
       
       btn_search.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			vf_top.setDisplayedChild(EDIT_TEXT_TOP_LAYOUT);
			//vf_bottom.setDisplayedChild(SECOND_CONTROL_BOTTOM_LAYOUT);
			edit_search.requestFocus();
		}
	});
       
       btn_direction.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			vf_top.setDisplayedChild(TEXTVIEW_TOP_LAYOUT);
			vf_center.setDisplayedChild(DIRECT_OPTION_CENTER_LAYOUT);
			vf_bottom.setDisplayedChild(SECOND_CONTROL_BOTTOM_LAYOUT);
			
			if(start_from_myLocation)
				edit_pick_start_point.setText("My Location");
			
			
		}
	});
       
       
       btn_getDirection.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			Geocoder gc = new Geocoder(MyMap_Beta.this);
			btn_direction_list.setEnabled(false);
			List<Address> lstFoundAddress, lstFoundAddresses2;
			Address returnAddress;
			
			try { 
				if(edit_pick_start_point.getText().toString().trim().compareTo("")!=0
						&& edit_pick_end_point.getText().toString().trim().compareTo("")!=0)
				{ 
					if(edit_pick_start_point.getText().toString().trim().compareTo("My Location") != 0)
					{
						lstFoundAddress = gc.getFromLocationName(edit_pick_start_point.getText().toString(), 1);
						returnAddress = lstFoundAddress.get(0);
				
						aLocation.setLatitude(returnAddress.getLatitude());
						aLocation.setLongitude(returnAddress.getLongitude());
				
					}
					else if(edit_pick_start_point.getText().toString().trim().compareTo("My Location") == 0)
					{
						
						aLocation.setLatitude(myCustomLocationOverlay.getMyLocation().getLatitudeE6()/1E6);
						aLocation.setLongitude(myCustomLocationOverlay.getMyLocation().getLongitudeE6()/1E6);
					}
				
					lstFoundAddresses2 =  gc.getFromLocationName(edit_pick_end_point.getText().toString(), 1);
				
					RadioGroup radioGroup = (RadioGroup)findViewById(R.id.direction_mode);
					
					  mode = radioGroup.getCheckedRadioButtonId();
					
					if(lstFoundAddresses2.size() > 0)
					{
						Toast.makeText(MyMap_Beta.this, "Getting direction", Toast.LENGTH_SHORT).show();
						returnAddress = lstFoundAddresses2.get(0); 						
						bLocation.setLatitude(returnAddress.getLatitude());
						bLocation.setLongitude(returnAddress.getLongitude());
					
						new GetDirection().execute();
					
						vf_center.setDisplayedChild(MAP_VIEW_CENTER_LAYOUT);
						vf_bottom.setDisplayedChild(THIRD_CONTROL_BOTTOM_LAYOUT);
						vf_top.setDisplayedChild(TEXTVIEW_TOP_LAYOUT);
					
						A_Overlay.setGeoPoint(new GeoPoint((int)(aLocation.getLatitude()*1E6), (int)(aLocation.getLongitude()*1E6)));
						listOfOverlays.add(A_Overlay);
					
						B_Overlay.setGeoPoint(new GeoPoint((int)(bLocation.getLatitude()*1E6), (int)(bLocation.getLongitude()*1E6)));
						listOfOverlays.add(B_Overlay);
						mc.animateTo(new GeoPoint((int)(aLocation.getLatitude()*1E6), (int)(aLocation.getLongitude()*1E6)));
					}					
					else 
					{
						Toast.makeText(MyMap_Beta.this, "Not Found Address", Toast.LENGTH_SHORT).show();
					} 
				}
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
	});
       
       btn_getDirection1.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			aLocation.setLatitude(myCustomLocationOverlay.getMyLocation().getLatitudeE6()/1E6);
			aLocation.setLongitude(myCustomLocationOverlay.getMyLocation().getLongitudeE6()/1E6);
			new GetDirection().execute();
			
			vf_bottom.setDisplayedChild(THIRD_CONTROL_BOTTOM_LAYOUT);
			
		}
	});
       
       btn_weather.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			getWeatherInfo();
		}
	});
        
	}// End init Component
	
	
	
	private void getWeatherInfo()
	{
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				try {
					
					
					URL url = new URL("http://api.wunderground.com/api/24a69b0605156b47/forecast/lang:VU/q/"+
							gp.getLatitudeE6()/1E6+","+gp.getLongitudeE6()/1E6+".xml");
					
					
					InputStream is = url.openConnection().getInputStream();
					if(is != null)
					{
					DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
					Document dom = db.parse(is);
					Element docEle = dom.getDocumentElement();
					NodeList nl = docEle.getElementsByTagName("forecastday");
					
					Element day_weather = (Element) nl.item(0); // 0 mean day of today
					Element title = (Element)day_weather.getElementsByTagName("title").item(0);
					Element fcttext =(Element)day_weather.getElementsByTagName("fcttext_metric").item(0);
					Element imageUrl	=(Element)day_weather.getElementsByTagName("icon_url").item(0);
					
					
					final String titleValue = title.getFirstChild().getNodeValue();
					final String detailValue = fcttext.getFirstChild().getNodeValue();
					final String imageUrlValue	= imageUrl.getFirstChild().getNodeValue();
					
					
					//Toast.makeText(MyMap_Beta.this, ""+titleValue+"\n"+fcttextValue, Toast.LENGTH_LONG);
					
					Log.i("<<Weather Update>>",""+titleValue+"\n"+detailValue );
					
					
					
					
					weather_title.post(new Runnable() {
						
						public void run() {
							// TODO Auto-generated method stub
							weather_title.setText(titleValue);
						}
					});
					
					
					weather_image.post(new Runnable() {
						Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUrlValue).getContent());
						public void run() {
							// TODO Auto-generated method stub
							
							weather_image.setImageBitmap(bitmap);
							
						}
					});
					
					final String editDetail = detailValue.replace("Cao", "\nNhiá»‡t Ä‘á»™ ");
					weather_detail.post(new Runnable() {
						
						public void run() {
							// TODO Auto-generated method stub
							weather_detail.setText(editDetail);
						}
					});
					
					}
					
					
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		
		vf_center.setDisplayedChild(WEATHER_VIEW_CENTER_LAYOUT);
		txt_info.setText("Thá»�i Tiáº¿t");
		vf_top.setDisplayedChild(TEXTVIEW_TOP_LAYOUT);
		vf_bottom.setDisplayedChild(4);
		
		
	}
	
	
	
	private void goToMyLocation()
	{
		GeoPoint currGP = myCustomLocationOverlay.getMyLocation();
		if(currGP != null)
		{
			mc.animateTo(currGP);
			mc.setZoom(17);
		}
		else
			Toast.makeText(MyMap_Beta.this, "Cannot determind location", Toast.LENGTH_SHORT).show();
	}
	
	
	private void promptEnableGPS() {
		LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
		boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

		// Check if enabled and if not send user to the GSP settings
		// Better solution would be to display a dialog and suggesting to 
		// go to the settings
		if (!enabled) {
		  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		  startActivity(intent);
		} 
	}
	
	
	private void resetSomething()
	{
		vf_top.setDisplayedChild(TEXTVIEW_TOP_LAYOUT);
		vf_center.setDisplayedChild(MAP_VIEW_CENTER_LAYOUT);
		vf_bottom.setDisplayedChild(FIRST_CONTROL_BOTTOM_LAYOUT);
		
		aLocation.reset();
		bLocation.reset();
		
		listOfOverlays.remove(A_Overlay);
		listOfOverlays.remove(B_Overlay);
		listOfOverlays.remove(routeOverlay);
		
		
		
		edit_search.setText("");
		edit_pick_start_point.setText("My Location");
		edit_pick_end_point.setText("");
		txt_info.setText("Hi!Map");
		
		start_from_myLocation = true;
		
	}
	
	
	///////////////////////////
	
	class GetDirection extends AsyncTask<Void, Void, Route> {
		
		
		@Override
		protected Route doInBackground(Void... params) {
			Parser parser;
	        String jsonURL = "http://maps.google.com/maps/api/directions/json?";
	        final StringBuffer sBuf = new StringBuffer(jsonURL);
	        sBuf.append("origin=");
	        sBuf.append(aLocation.getLatitude());
	        sBuf.append(',');
	        sBuf.append(aLocation.getLongitude());
	        sBuf.append("&destination=");
	        sBuf.append(bLocation.getLatitude());
	        sBuf.append(',');
	        sBuf.append(bLocation.getLongitude());
	        
	        sBuf.append("&sensor=true&mode=driving");
	        
	        if (mode == 1) {
	        	sBuf.append("transit");
	        } else if (mode == 2){
	        	sBuf.append("walking");
	        } else {
	        	sBuf.append("driving");
	        }
	        
	        Log.d("ABC", sBuf.toString());
	        
	        parser = new GoogleParser(sBuf.toString());
	        Route r =  parser.parse();
	        
	        return r;
		}
		
		@Override
		protected void onPostExecute(Route result) {
			Toast.makeText(MyMap_Beta.this, "Đã xong", Toast.LENGTH_LONG).show();
			
			if(result == null) return;
			btn_direction_list.setEnabled(true);	
			info_direction = result.getName();					
			 distance = result.getLength();
			 duration = result.getTime();
			
			Log.i("xxx",""+  distance + ", " + duration);
			
			List<Segment> directionSeg = result.getSegments();
			
			directionList = new ArrayList<String>();
			
			for (int i = 0 ; i < directionSeg.size() ; i++) {
				directionList.add(directionSeg.get(i).getInstruction());
			}		
			routeOverlay = new RouteOverlay(result, Color.rgb(132,112,255));
			listOfOverlays.add(routeOverlay);
			 
		}
	}



	
    
}
